//
//  SJ_CallDetailVC.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/12/13.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "SJ_CallDetailVC.h"


#import "PlaceholderTextView.h"
#import "KSDatePicker.h"
#import "TodoHttpManager.h"


@interface SJ_CallDetailVC ()<UITextViewDelegate>
{
    UILabel *_dayLab;//跟进日期
    UILabel *_lastLab;//提醒日期
    NSString *up;//开关的状态
    NSString *genTime;
}

@property(nonatomic,strong)PlaceholderTextView *textVi;


@property(nonatomic,strong)UIView *fiestView;
@property(nonatomic,strong)UIView *lastView;

@property(nonatomic,strong)UISwitch *oneSwi;


@end

@implementation SJ_CallDetailVC

-(void)viewWillAppear:(BOOL)animated
{
    
    //跟进的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDGuPhoneGen:) name:TDGuPhoneGen object:nil];
    
    
}
-(void)didTDGuPhoneGen:(NSNotification *)notifi
{
    if (notifi.object !=nil)
    {
        NSString *sta = [notifi.object objectForKey:@"status"];
        if ([sta isEqualToString:@"1"])
        {
            //            NewGenJinVC *newVC = [[NewGenJinVC alloc] init];
            //            [self.navigationController pushViewController:newVC animated:NO];
            [self.navigationController popViewControllerAnimated:NO];
        }
        else
        {
            [_manager MakeMyselfAlert:sta andIsNeedTitle:nil andShowTime:2.0];
        }
    }
    
}
-(void)viewDidDisappear:(BOOL)animated
{
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"跟进";
    
    self.view.backgroundColor = BgColor;
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:@selector(wanCheng)];
    rightBarItem.tintColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItem = rightBarItem;
    [self.navigationItem.rightBarButtonItem setTitle:@"完成"];
    
    _manager = [TodoHttpManager sharedManager];
    
    
    //大的View 包括框
    [self setUpFiestView];
    
    //提醒
    [self setupRemindMe];
}

-(void)setUpFiestView
{
    _fiestView = [[UIView alloc] init];
    _fiestView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_fiestView];
    [_fiestView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(B_WIDTH, 410));
        make.centerX.equalTo(self.view.mas_centerX);
        make.left.equalTo(self.view);
        make.top.equalTo(self.view.mas_top).offset(64);
    }];
    
    //获取当前时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    genTime = [formatter stringFromDate:[NSDate date]];
    
    _dayLab = [[UILabel alloc] init];
    _dayLab.textColor = [UIColor blackColor];
    _dayLab.font = [UIFont systemFontOfSize:12];
    _dayLab.text = [NSString stringWithFormat:@"跟进日期 : %@",genTime];
    [_fiestView addSubview:_dayLab];
    [_dayLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(300, 16));
        make.left.equalTo(_fiestView.mas_left).offset(25);
        make.top.equalTo(_fiestView.mas_top).offset(10);
    }];
    
    UILabel *lineLab = [[UILabel alloc] init];
    lineLab.backgroundColor = [UIColor colorWithHexString:@"#d9d9d9"];
    [_fiestView addSubview:lineLab];
    [lineLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(B_WIDTH, 0.5f));
        make.centerX.equalTo(_fiestView.mas_centerX);
        make.top.mas_equalTo(_dayLab.mas_bottom).offset(10);
        
    }];
    
    
    
    
    _textVi = [[PlaceholderTextView alloc] init];
    _textVi.delegate = self;
    _textVi.font = [UIFont systemFontOfSize:12];
    _textVi.textColor = [UIColor blackColor];
    _textVi.textAlignment = NSTextAlignmentLeft;
    _textVi.editable = YES;
    up = @"1";
    _textVi.layer.masksToBounds = YES;
    _textVi.layer.cornerRadius  = 10;
    _textVi.layer.borderColor = [[UIColor grayColor]CGColor];
    _textVi.layer.borderWidth = 1.0f;
    [_fiestView addSubview:_textVi];
    [_textVi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(B_WIDTH-25*2, 340));
        make.left.equalTo(_fiestView.mas_left).offset(25);
        make.top.equalTo(lineLab.mas_bottom).offset(15);
    }];
    
    
    
    
}

#pragma mark 提醒
-(void)setupRemindMe
{
    _lastView = [[UIView alloc] init];
    _lastView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_lastView];
    [_lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(B_WIDTH, 80));
        make.centerX.equalTo(self.view.mas_centerX);
        make.left.equalTo(self.view);
        make.top.equalTo(_fiestView.mas_bottom).offset(10);
    }];
    
    UILabel *lineLab = [[UILabel alloc] init];
    lineLab.backgroundColor = [UIColor colorWithHexString:@"#d9d9d9"];
    [_lastView addSubview:lineLab];
    [lineLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(B_WIDTH, 0.5f));
        make.centerX.equalTo(_lastView.mas_centerX);
        make.top.mas_equalTo(_lastView.mas_bottom).offset(-40);
        
    }];
    
    UIImageView *imgA = [[UIImageView alloc] init];
    imgA.image = [UIImage imageNamed:@"Care_clock"];
    [_lastView addSubview:imgA];
    [imgA mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.left.equalTo(_lastView.mas_left).offset(25);
        make.top.equalTo(_lastView.mas_top).offset(9);
    }];
    
    UILabel *oneLab = [[UILabel alloc] init];
    oneLab.text = @"在指定日期提醒我";
    oneLab.textColor = [UIColor orangeColor];
    oneLab.font = [UIFont systemFontOfSize:12];
    [_lastView addSubview:oneLab];
    [oneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(120, 15));
        make.left.mas_equalTo(imgA.mas_right).offset(10);
        make.top.equalTo(_lastView.mas_top).offset(12);
        
        
    }];
    
    
    
    _oneSwi = [[UISwitch alloc] init];
    //开关状态设置属性
    //YES:开启状态
    //NO:关闭状态
    _oneSwi.on=YES;
    
    //onTintColor处于on时switch 的颜色
    _oneSwi.onTintColor = [UIColor orangeColor];
    //tintColor处于off时switch 的颜色
    _oneSwi.tintColor = [UIColor grayColor];
    _oneSwi.transform = CGAffineTransformMakeScale( 0.7, 0.7);//缩放
    [_oneSwi addTarget:self action:@selector(onSwitchRemind:) forControlEvents:UIControlEventValueChanged];
    [_lastView addSubview:_oneSwi];
    [_oneSwi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(40, 20));
        make.right.equalTo(_lastView.mas_right).offset(-30);
        make.top.equalTo(_lastView.mas_top).offset(8);
    }];
    
    
    //获取当前时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    _lastLab = [[UILabel alloc] init];
    _lastLab.textColor = [UIColor orangeColor];
    _lastLab.text = [NSString stringWithFormat:@"%@",dateTime];
    _lastLab.font = [UIFont systemFontOfSize:12];
    [_lastView addSubview:_lastLab];
    [_lastLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(120, 20));
        make.left.mas_equalTo(_lastView.mas_left).offset(30);
        make.top.equalTo(lineLab.mas_bottom).offset(8);
        
        
    }];
    
    UIButton *keBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [keBtn setBackgroundImage:[UIImage imageNamed:@"btnRiLi"] forState:0];
    [keBtn addTarget:self action:@selector(riLiGuanHuai:) forControlEvents:UIControlEventTouchUpInside];
    [_lastView addSubview:keBtn];
    [keBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(30, 20));
        make.right.equalTo(_lastView.mas_right).offset(-30);
        make.top.equalTo(lineLab.mas_bottom).offset(8);
    }];
    
    
    
    
    
    
    
    
    
    
    
    
    
}

#pragma mark 开关按钮
-(void)onSwitchRemind:(UISwitch *)onSwitch
{
    if (onSwitch.on == YES)
    {
        NSLog(@"开关被打开");
        up = @"1";
    }
    else
    {
        NSLog(@"开关关闭");
        up = @"0";
    }
}

#pragma mark 日历按钮
-(void)riLiGuanHuai:(UIButton *)btn
{
    KSDatePicker* picker = [[KSDatePicker alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 40, 300)];
    picker.appearance.radius = 5;
    
    //设置回调
    picker.appearance.resultCallBack = ^void(KSDatePicker* datePicker,NSDate* currentDate,KSDatePickerButtonType buttonType){
        
        if (buttonType == KSDatePickerButtonCommit) {
            
            NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd"];
            
            _lastLab.text = [formatter stringFromDate:currentDate];
            
            
        }
    };
    // 显示
    [picker show];
}

#pragma mark 导航栏右侧----------完成
-(void)wanCheng
{
    
    NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
    
    NSString *uid= [userDefa objectForKey:@"USER_ID"];
   // [_manager userGMKeLaiFangGenWithID:_IdGen withGenTime:genTime withHuiMessage:_textVi.text withNextTime:_lastLab.text withUserID:uid withType:@"1" withIsTrue:up];
    
    
  
    
    [_manager userGuWenPhoneZengID:_IdGen withGenTime:genTime withHuiMessage:_textVi.text withNextTime:_lastLab.text withUserID:uid withType:@"1" withIsTrue:up];
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
